package com.lsh.queue;

import java.util.Scanner;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/7/29 10:14 上午
 * @desc ：使用数组实现队列
 *
 */
public class ArrayQueue {
    public static void main(String[] args) {
        //创建一个队列
        ArrayQueueDemo queue = new ArrayQueueDemo(3);
        char key = ' ';// 用于接收用户输入信息
        Scanner scanner = new Scanner(System.in);
        Boolean loop = true;
        while (loop){
            System.out.println("s(show()):显示队列");
            System.out.println("a(add()):添加数据到队列");
            System.out.println("g(get()):获得队列中数据");
            System.out.println("e(exit()):退出程序");
            System.out.println("h(head()):查看队列头");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int i = scanner.nextInt();
                    queue.add(i);
                    break;
                case 'g':
                    try {
                        int i2 = queue.get();
                        System.out.printf("取出的元素是%d\n",i2);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int head = queue.head();
                        System.out.printf("队列头是%d\n",head);
                    }catch (RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
            }
        System.out.println("程序退出");
        }
    }
class  ArrayQueueDemo {
    private int mixSize;//最大容量
    private int start;//队列头
    private int end;//队列尾
    private int[] arr;//该数组用于存放数据
    /**
     * 创建队列的构造器
     * @param arrMaxSize
     */
    public ArrayQueueDemo(int arrMaxSize) {
        mixSize = arrMaxSize;
        arr = new int[arrMaxSize];
        start = -1;
        end= -1;
    }
    /**
     * 判断队列是否已满
     * 如果队列尾大于等于 数组的最大索引的话（即长度减一），队列就是满了
     * @return
     */
    public boolean isFull(){
        return end >= mixSize-1;
    }
    /**
     * 判断队列是否为空
     * 如果队列头和队列尾相等的话，队列就是空
     * @return
     */
    public boolean isEmpty(){
        return start == end;
    }
    /**
     * 往队列里添加数据
     * 先判断队列是否已满
     * 未满的话，队列尾加一  进行添加
     * @param i
     */
    public void add(int i)  {
        if (isFull()){
            System.out.println("队列已满！");
            return;
        }
            end++;
            arr[end] = i;
    }
    /**
     * 取出队列的数据
     */
    public int get(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空！");
        }
        start++;
        return arr[start];
    }
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空！");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }
    public int head(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        return arr[start+1];
    }
}
